package xyz.chenyongpeng.orderproviderdubbo.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import xyz.chenyongpeng.entity.ZsCouponCode;
import xyz.chenyongpeng.entity.ZsCouponHistory;
import xyz.chenyongpeng.orderproviderdubbo.mapper.ZsCouponCodeMapper;
import xyz.chenyongpeng.orderproviderdubbo.mapper.ZsCouponHistoryMapper;
import xyz.chenyongpeng.service.IZsCouponCodeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import xyz.chenyongpeng.utils.IDUtils;

import java.util.List;

/**
 * <p>
 * 优惠券分配表 服务实现类
 * </p>
 *
 * @author chenyongpeng
 * @since 2018-12-10
 */
public class ZsCouponCodeService extends ServiceImpl<ZsCouponCodeMapper, ZsCouponCode> implements IZsCouponCodeService {

    @Autowired
    private ZsCouponCodeMapper zsCouponCodeMapper;

    @Autowired
    private ZsCouponHistoryMapper zsCouponHistoryMapper;

    @Override
    public List<String> getUserAllCoupons(String u_id) {
        return zsCouponCodeMapper.getUserAllCouponCode(u_id);
    }

    @Override
    public boolean saveCoupon(ZsCouponCode zsCouponCode) {
        String code = IDUtils.createID();
        zsCouponCode.setIsUsed(0);
        zsCouponCode.setAddTime((int)(System.currentTimeMillis() / 1000));
        zsCouponCode.setIsDelete(0);
        zsCouponCode.setCode(code);
        int count = zsCouponCodeMapper.insert(zsCouponCode);
        if (count > 1){
            ZsCouponHistory zsCouponHistory = new ZsCouponHistory();
            zsCouponHistory.setCodeId(Integer.parseInt(code));
            zsCouponHistory.setTxBehavior("优惠券发放成功");
            zsCouponHistory.setUId(zsCouponCode.getUId());
            zsCouponHistory.setIsSendUsed(1);
            zsCouponHistory.setAddTime((int)(System.currentTimeMillis() /1000));
            zsCouponHistoryMapper.insert(zsCouponHistory);

            return true;
        }else {
            return false;
        }
    }
}
